<style>
	.hide-element { width: 100%; position: absolute; z-index: -1; top: -1000rpx; left: 0; opacity: 0; background: rgba(0, 0, 0, 0); }
	.share-codeurl { }
	.share-codeurl .cell { margin: 20rpx 20rpx 0; padding: 30rpx 20rpx; line-height: 40rpx; font-size: 30rpx; background-color: #f8f8f8; border-radius: 20rpx; }
	.share-codeurl .cell .note { width: 220rpx; color: #999; }
	.share-codeurl .cell .desc { word-break: break-all; }
	.share-btns { padding: 20rpx 0 40rpx; }
	.share-btns .icon { margin: 0 auto; width: 56rpx; height: 56rpx; }
	.share-btns .desc { padding-top: 4rpx; color: #333; font-size: 28rpx; }
</style>

<template>
	<view style="background-color: #fff;">

		<swiper v-if="shareQrcodeImages.length" :indicator-dots="true" :autoplay="false" :interval="2000" :duration="500" :style="'height: '+shareSize.height+'px;'" @change="swiperChange">
			<swiper-item v-for="(item, index) in shareQrcodeImages" :key="index">
				<image mode="widthFix" :src="item" style="width: 100%; height: auto;"></image>
			</swiper-item>
		</swiper>

		<image class="hide-element share-img" :src="shareImages[0]" mode="widthFix"></image>
		<canvas class="hide-element" canvas-id="qrcode" :style="{ width: `240rpx`, height: `240rpx` }"></canvas>

		<block v-for="(item, index) in shareImages" :key="index">
			<canvas class="hide-element" :canvas-id="'banner'+index" :style="{ width: shareSize.width + 'px', height: shareSize.height + 'px' }"></canvas>
		</block>

		<view v-if="wxShareTips" style=" padding: 20rpx 0 10rpx; text-align: center; color: #999;">长按图片进行分享</view>

		<view class="share-codeurl">
			<view class="cell" @click="$common.doCopy(userInfo.customerCode)">
				<view class="flex2">
					<view class="flex-0 note">返佣邀请码</view>
					<view class="flex-1 desc">{{userInfo.customerCode}}</view>
				</view>
			</view>
			<view class="cell" @click="$common.doCopy(shareUrl)">
				<view class="flex2" style="align-items:center;">
					<view class="flex-0 note">返佣邀请链接</view>
					<view class="flex-1 desc">{{shareUrl}}</view>
				</view>
			</view>
		</view>

		<view v-if="shareBtnVisible && shareQrcodeImages.length" class="share-btns">
			<view class="flex2">
				<block v-for="(item, index) in shareType" :key="index">
					<view class="flex-1" style=" padding: 20rpx 0 10rpx; text-align: center;" @tap="doShare(item.id)">
						<view class="icon">
							<image mode="scaleToFill" class="image" :src="item.src"></image>
						</view>
						<view class="desc">{{ item.name }}</view>
					</view>
				</block>
			</view>
		</view>
		
		<view style="height: 20rpx;"></view>

	</view>
</template>

<script>
	import indexConfig from '@/config/index.config';
	import uQRCode from "@/utils/uqrcode/uqrcode.js"

	export default {
		data() {
			return {
				shareUrl: '',
				shareSize: {},
				shareIndex: 0,
				shareImages: [
					'/static/image/share.jpg',
				],
				shareQrcodeImages: [],
				shareQrcodeImage: '',

				wxShareTips: false,

				shareBtnVisible: false,
				shareType: [{
						id: 1,
						visible: false,
						name: '下载',
						src: '/static/image/share-download.png'
					},
					{
						id: 2,
						visible: false,
						name: '分享',
						src: '/static/image/share-share.png'
					},
				],
			}
		},
		computed: {
			userInfo() {
				let userInfo = this.$mStore.state.userInfo
				this.shareUrl = 'https://app.xfdj.klt.ink/pages/login/login?scode=' + userInfo.customerCode
				return userInfo
			},
		},
		onLoad(option) {
			uni.showLoading({
				title: '正在加载'
			})
			setTimeout(() => {
				uni.hideLoading()
			}, 2000)

			// #ifdef APP-PLUS
			this.shareBtnVisible = true
			this.shareType[0].visible = true
			this.shareType[1].visible = true
			// #endif

			//  #ifdef H5
			if (/(MicroMessenger)/i.test(navigator.userAgent)) {
				this.wxShareTips = true
			}
			// #endif

			this.$mStore.commit('getUserInfo')
			this.createQrcode(0)
		},
		onShow() {

		},
		methods: {
			swiperChange(e) {
				this.shareIndex = e.detail.current
			},
			createQrcode(index) {
				if (!this.userInfo.id || !this.shareImages.length) {
					return false
				}

				uQRCode.make({
					canvasId: 'qrcode',
					text: this.shareUrl,
					size: uni.upx2px(240),
					margin: 8,
					success: (res) => {
						this.shareQrcodeImage = res
						setTimeout(() => {
							uni.createSelectorQuery().select('.share-img').boundingClientRect(size => {
								this.shareSize = size

								this.shareImages.forEach((item, index) => {
									// #ifdef APP
									this.shareImages[index] = indexConfig.hostUrl + item
									// #endif
									this.createShareImage(index)
								})

							}).exec()
						}, 300)
					},
					fail() {
						uni.hideLoading()
						console.log('createQrcode', createQrcode)
					}
				})
			},
			createShareImage(index) {
				uni.downloadFile({
					url: this.shareImages[index],
					success: ({
						tempFilePath
					}) => {
						let _q_x = this.shareSize.width / 2 - uni.upx2px(240 / 2) + uni.upx2px(220)
						let _q_y = this.shareSize.height * 0.76

						let ctx = uni.createCanvasContext('banner' + index)
						ctx.drawImage(tempFilePath, 0, 0, this.shareSize.width, this.shareSize.height)
						ctx.drawImage(this.shareQrcodeImage, _q_x, _q_y, uni.upx2px(240), uni.upx2px(240))
						ctx.restore()
						ctx.draw(true, () => {
							uni.canvasToTempFilePath({
								canvasId: 'banner' + index,
								width: this.shareSize.width,
								height: this.shareSize.height,
								destWidth: this.shareSize.width * 2,
								destHeight: this.shareSize.height * 2,
								success: (res) => {
									uni.hideLoading()
									this.shareQrcodeImages.push(res.tempFilePath)
								},
								fail: (e) => {
									console.log('createShareImage', e)
								}
							})
						})
					}
				})
			},
			doShare(id) {
				uni.showLoading({
					title: '正在加载'
				})
				setTimeout(() => {
					uni.hideLoading()
				}, 500)

				if (!this.userInfo.id) {
					uni.showToast({
						title: '请先登录',
						icon: 'none'
					})
					return
				}

				if (id == 1) {
					uni.saveImageToPhotosAlbum({
						filePath: this.shareQrcodeImages[this.shareIndex],
						success: (res) => {
							uni.showToast({
								title: '成功保存到相册',
								icon: 'none'
							})
						}
					})
				} else if (id == 2) {
					uni.share({
						provider: "weixin",
						scene: "WXSceneSession",
						type: 2,
						imageUrl: this.shareQrcodeImages[this.shareIndex],
						success: (res) => {
							console.log("success:" + JSON.stringify(res));
						},
						fail: (err) => {
							console.log("fail:" + JSON.stringify(err));
						}
					})
				}
			},
		}
	}
</script>
